//
//  ByUIAccountsSecurityVC.m
//  itboye_shop_ios
//
//  Created by justiOS on 17/2/14.
//  Copyright © 2017年 com.itboye. All rights reserved.
//

#import "ByUIAccountsSecurityVC.h"
// net
#import "ByApiUser.h"
// vc
#import "ByUIChangePwdVC.h"
@interface ByUIAccountsSecurityVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *table;
@property (nonatomic, strong) NSArray *nameAry;

@end

@implementation ByUIAccountsSecurityVC
#pragma mark getter
- (UITableView *)table {
    if (!_table) {
        _table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStylePlain];
        _table.delegate = self;
        _table.dataSource = self;
        _table.separatorStyle = UITableViewCellSeparatorStyleNone;
        _table.backgroundColor = TABLE_COLOR;
    }
    return _table;
}

- (NSArray *)nameAry {
    if (!_nameAry) {
        _nameAry = @[@"会员名",@"修改密码",@"注销账户"];
    }
    return _nameAry;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"账户与安全";
    [self addLeftBackBarButtonItem];
    [self.view addSubview:self.table];
    
}

#pragma mark tableView Delegate and DateSource

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"normalCell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"normalCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    cell.textLabel.text = self.nameAry[indexPath.row];
    cell.textLabel.font = FONT(16);
    cell.textLabel.textColor = [UIColor darkGrayColor];
    
    if (indexPath.row == self.nameAry.count - 1) {
        
    }else{
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(10, 43.5, SCREEN_WIDTH - 10, 0.5)];
        line.backgroundColor = BY_LINE_COLOR;
        [cell addSubview:line];
    }
    if (indexPath.row == 0) {
        cell.detailTextLabel.text = BY_USER.mobile;
    }else{
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.nameAry.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
      
    }else if (indexPath.row == 1){
        ByUIChangePwdVC *vc = [[ByUIChangePwdVC alloc] init];
        [self by_normalPush:vc hasNavi:YES];
    }else if (indexPath.row == 2){
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"确定注销吗" message:@"注销后将无法恢复账号" preferredStyle:UIAlertControllerStyleActionSheet];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"删除" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
            [self deleteAccounts];
        }];
        [alertController addAction:cancelAction];
        [alertController addAction:okAction];
        [self presentViewController:alertController animated:YES completion:nil];
    }
}

- (void)deleteAccounts {
    ByNetParamsModel *pm = [[ByNetParamsModel alloc] init];
    pm.mobile = BY_USER.mobile;
    [ByApiUser deleteAccounts:pm success:^(ByNetBaseResult *result) {
        [ByCoreToolAlert showOnlyText:result.msg];
        [ByCacheUser setUserInfo:nil];
        [self by_normalPopVC:@"ByUIPersonSettingVC"];
    } failure:^(ByNetBaseResult *result) {
        [ByCoreToolAlert showOnlyText:result.msg];
    }];
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
